home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /*
- * cut/paste example using Motif Clipboard
- */
-
-
- #include <X11/Intrinsic.h>
- #include <X11/StringDefs.h>
- #include <Xm/Xm.h>
- #include <Xm/Text.h>
- #include <Xm/RowColumn.h>
- #include <Xm/MainW.h>
- #include <Xm/CascadeB.h>
- #include <Xm/BulletinB.h>
- #include <Xm/PushB.h>
- #include <Xm/CutPaste.h>
-
- #define MAXCHR 1024
-
- char *selection_buffer1 = NULL; /* malloc'd space to store data whose */
- /* name was passed to Motif Clipboard */
-
- void quitCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data);
- void copyByNameCB(Widget w, caddr_t client_data,
- XmAnyCallbackStruct *call_data);
- void copyNamedDataCB(Widget w, int *data_id, int *private, int *reason);
- void copyDataCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data);
- void pasteDataCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data);
- void undoCopyCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data);
-
- void inquireCountCB(Widget w, caddr_t client_data,
- XmAnyCallbackStruct *call_data);
- void inquireFormatCB(Widget w, caddr_t client_data,
- XmAnyCallbackStruct *call_data);
- void inquireLengthCB(Widget w, caddr_t client_data,
- XmAnyCallbackStruct *call_data);
- void inquirePendingItemsCB(Widget w, caddr_t client_data,
- XmAnyCallbackStruct *call_data);
-
- /* ======================== */
- void
- main(int argc, char **argv)
- {
- Arg args[20];
- Cardinal argcount;
- Widget main_widget, main_window, menu_bar, new_menu, text_widget;
- Widget cascade1, cascade2, cascade3, pulldown1, pulldown2, pulldown3;
- Widget pb1, pb2, pb3, pb4, pb5, pb6, pb7, pb8, pb9;
-
- main_widget = XtInitialize(argv[0], "ClipTest", NULL, 0, &argc, argv);
-
- main_window = XmCreateMainWindow(main_widget, "Main", NULL, 0);
- XtManageChild(main_window);
-
- menu_bar = XmCreateMenuBar(main_window, "Menubar", NULL, 0);
- XtManageChild(menu_bar);
-
- argcount = 0;
- XtSetArg(args[argcount], XmNeditable, True); argcount++;
- XtSetArg(args[argcount], XmNeditMode, XmMULTI_LINE_EDIT); argcount++;
- XtSetArg(args[argcount], XmNcolumns, 60); argcount++;
- XtSetArg(args[argcount], XmNrows, 15); argcount++;
- XtSetArg(args[argcount], XmNx, 15); argcount++;
- XtSetArg(args[argcount], XmNy, 15); argcount++;
- text_widget = XmCreateText(main_window, "TextWidget", args, argcount);
- XtManageChild(text_widget);
-
- XmMainWindowSetAreas(main_window, menu_bar, NULL, NULL, NULL, text_widget);
-
- /* create the menus */
- pulldown1 = XmCreatePulldownMenu(menu_bar, "pulldown1", NULL, 0);
- pulldown2 = XmCreatePulldownMenu(menu_bar, "pulldown2", NULL, 0);
- pulldown3 = XmCreatePulldownMenu(menu_bar, "pulldown3", NULL, 0);
- XtSetArg(args[0], XmNsubMenuId, pulldown1);
- cascade1 = XmCreateCascadeButton(menu_bar, "File", args, 1);
- XtSetArg(args[0], XmNsubMenuId, pulldown2);
- cascade2 = XmCreateCascadeButton(menu_bar, "Edit", args, 1);
- XtSetArg(args[0], XmNsubMenuId, pulldown3);
- cascade3 = XmCreateCascadeButton(menu_bar, "Inquire", args, 1);
- XtManageChild(cascade1); XtManageChild(cascade2); XtManageChild(cascade3);
-
- /* put buttons in the menus */
- pb1 = XmCreatePushButton(pulldown1, "Quit", NULL, 0);
- XtAddCallback(pb1, XmNactivateCallback, quitCB, text_widget);
- XtManageChild(pb1);
-
- pb8 = XmCreatePushButton(pulldown2, "Copy Name", NULL, 0);
- XtAddCallback(pb8, XmNactivateCallback, copyByNameCB, text_widget);
- pb4 = XmCreatePushButton(pulldown2, "Copy Data", NULL, 0);
- XtAddCallback(pb4, XmNactivateCallback, copyDataCB, text_widget);
- pb5 = XmCreatePushButton(pulldown2, "Paste", NULL, 0);
- XtAddCallback(pb5, XmNactivateCallback, pasteDataCB, text_widget);
- pb9 = XmCreatePushButton(pulldown2, "Undo Copy", NULL, 0);
- XtAddCallback(pb9, XmNactivateCallback, undoCopyCB, text_widget);
- XtManageChild(pb4); XtManageChild(pb5);
- XtManageChild(pb8); XtManageChild(pb9);
-
- pb2 = XmCreatePushButton(pulldown3, "Count", NULL, 0);
- XtAddCallback(pb2, XmNactivateCallback, inquireCountCB, text_widget);
- pb3 = XmCreatePushButton(pulldown3, "Format", NULL, 0);
- XtAddCallback(pb3, XmNactivateCallback, inquireFormatCB, text_widget);
- pb6 = XmCreatePushButton(pulldown3, "Length", NULL, 0);
- XtAddCallback(pb6, XmNactivateCallback, inquireLengthCB, text_widget);
- pb7 = XmCreatePushButton(pulldown3, "PendingItems", NULL, 0);
- XtAddCallback(pb7, XmNactivateCallback, inquirePendingItemsCB, text_widget);
- XtManageChild(pb2); XtManageChild(pb3);
- XtManageChild(pb6); XtManageChild(pb7);
-
-
- XtRealizeWidget(main_widget);
- XtMainLoop();
-
- }
-
- /*
- ***** end of main, start of callbacks
- */
-
- void copyByNameCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data)
- {
- char *selection;
- long item_id, data_id;
- XButtonEvent *button_event = (XButtonEvent *) call_data->event;
- Widget w_text = (Widget) client_data;
- static int private = 1;
-
- if((selection = XmTextGetSelection(w_text)) == NULL) return ;
- if((void *)selection_buffer1 != NULL) free((void *)selection_buffer1);
- selection_buffer1 = (char *)malloc(strlen(selection)+1);
- strcpy(selection_buffer1, selection);
-
- while(XmClipboardStartCopy(XtDisplay(w), XtWindow(w_text), "ClipDemo",
- button_event->time, w_text, copyNamedDataCB, &item_id)
- != ClipboardSuccess) ;
-
- while(XmClipboardCopy(XtDisplay(w), XtWindow(w_text), item_id,
- "STRING", NULL, strlen(selection),
- private++, &data_id) != ClipboardSuccess) ;
-
- while(XmClipboardEndCopy(XtDisplay(w), XtWindow(w_text), item_id)
- != ClipboardSuccess) ;
-
- printf("%d >>%s<<\n",strlen(selection_buffer1),selection_buffer1);
- XtFree(selection);
- }
-
- void copyNamedDataCB(Widget w, int *data_id, int *private, int *reason)
- {
- Widget w_text = w;
-
- printf("copyNamedDataCB: data id %d, private %d\n ", *data_id, *private);
- printf("copyNamedDataCB: data = %s\n", selection_buffer1);
- if(*reason == XmCR_CLIPBOARD_DATA_DELETE)
- {
- printf("allowed to delete named data since its no longer needed. \n");
- free(selection_buffer1);
- return ;
- }
- printf("copying named data to the Motif Clipboard. \n");
- printf("%d >>%s<<\n",strlen(selection_buffer1),selection_buffer1);
- while(XmClipboardCopyByName(XtDisplay(w), XtWindow(w_text), *data_id,
- selection_buffer1, strlen(selection_buffer1),
- *private ) == ClipboardLocked ) ;
- printf("copyNamedDataCB: operation completed. \n");
- return ;
- }
-
- void copyDataCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data)
- {
- char *selection;
- long item_id, data_id;
- XButtonEvent *button_event = (XButtonEvent *) call_data->event;
- Widget w_text = (Widget) client_data;
-
- if((selection = XmTextGetSelection(w_text)) == NULL) return ;
-
- while(XmClipboardStartCopy(XtDisplay(w), XtWindow(w_text), "ClipDemo",
- button_event->time, w_text, NULL, &item_id) != ClipboardSuccess) ;
-
- while(XmClipboardCopy(XtDisplay(w), XtWindow(w_text), item_id,
- "STRING", selection, strlen(selection),
- 0, &data_id) != ClipboardSuccess) ;
-
- while(XmClipboardEndCopy(XtDisplay(w), XtWindow(w_text), item_id)
- != ClipboardSuccess) ;
-
- XtFree(selection);
- }
-
- void pasteDataCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data)
- {
- int id, insertion_point, stat, copy_done = FALSE, numchars;
- Widget w_text = (Widget) client_data;
- char buf[MAXCHR];
- XButtonEvent *button_event = (XButtonEvent *) call_data->event;
-
- printf("pasteDataCB: about to start \n");
- while(XmClipboardStartRetrieve(XtDisplay(w), XtWindow(w_text),
- button_event->time) == ClipboardLocked ) ;
-
- /* put any required clipboard inquire function calls HERE!! */
-
- while(!copy_done)
- {
- while((stat = XmClipboardRetrieve(XtDisplay(w), XtWindow(w_text),
- "STRING", buf, MAXCHR, &numchars, &id))
- == ClipboardLocked) ;
- switch(stat)
- {
- case ClipboardNoData:
- printf("pasteDataCB: no data to paste from clipboard \n");
- copy_done = TRUE;
- break;
-
- case ClipboardTruncate:
- printf(" more data remains to be copied from Clipboard \n");
- buf[numchars] = '\0';
- printf("pasteDataCB: buf = %s\n", buf);
- insertion_point = XmTextGetInsertionPosition(w_text);
- XmTextReplace(w_text, insertion_point, insertion_point, buf);
- copy_done = FALSE;
- break;
-
- case ClipboardSuccess:
- printf("pasteDataCB: got all available data from clipboard \n");
- buf[numchars] = '\0';
- printf("pasteDataCB: buf = %s\n", buf);
- insertion_point = XmTextGetInsertionPosition(w_text);
- XmTextReplace(w_text, insertion_point, insertion_point, buf);
- copy_done = TRUE;
- break;
- }
- }
- while(XmClipboardEndRetrieve(XtDisplay(w), XtWindow(w_text))
- == ClipboardLocked ) ;
- printf("pasteDataCB: operation completed \n");
- return ;
- }
-
- void undoCopyCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data)
- {
- Widget w_text = (Widget) client_data;
-
- printf("undoing the last copy by this app to the Motif Clipboard. \n");
- while(XmClipboardUndoCopy(XtDisplay(w), XtWindow(w_text))
- == ClipboardLocked ) ;
- return ;
- }
-
- void quitCB(Widget w, caddr_t client_data, XmAnyCallbackStruct *call_data)
- {
-
- int stat, flag = True;
- Widget w_text = (Widget) client_data;
- int index, i;
- unsigned long buffer_len = 101;
- unsigned long copied_len;
- char format_name_buf[101], str[101];
- unsigned long icount;
- XmClipboardPendingList ilist;
-
- if (selection_buffer1 != NULL)
- printf("%d >>%s<<\n",strlen(selection_buffer1),selection_buffer1);
- while(( stat = XmClipboardInquirePendingItems(XtDisplay(w),
- XtWindow(w_text), "STRING", &ilist, &icount ))
- == ClipboardLocked ) ;
- if(icount == 0)
- printf("There are no pending items for this format.\n");
- else {
- for( i = 0; i < icount; i++) {
- printf("\t pair # %d: data id %d | private id %d \n",
- i, (ilist+i)->DataId, (ilist+i)->PrivateId);
- while(XmClipboardCopyByName(XtDisplay(w), XtWindow(w_text),
- (ilist+i)->DataId, selection_buffer1,
- strlen(selection_buffer1),
- (ilist+i)->PrivateId ) == ClipboardLocked ) ;
- printf("%d >>%s<<\n",strlen(selection_buffer1),selection_buffer1);
- }
- XtFree(ilist);
- }
- XtCloseDisplay(XtDisplay(w));
- exit(0);
- }
-
- void inquireCountCB(Widget w, caddr_t client_data,
- XmAnyCallbackStruct *call_data)
- {
- int stat;
- Widget w_text = (Widget) client_data;
- int count, max_format_name_length;
-
- while(( stat = XmClipboardInquireCount(XtDisplay(w), XtWindow(w_text),
- &count, &max_format_name_length )) == ClipboardLocked ) ;
- if(stat == ClipboardNoData)
- printf("no data on Motif Clipboard\n");
- else
- {
- printf("\nMotif Clipboard has %d data items\n", count);
- printf(" and the max length of the format names is %d\n",
- max_format_name_length);
- }
- }
-
- void inquireFormatCB(Widget w, caddr_t client_data,
- XmAnyCallbackStruct *call_data)
- {
- int stat, flag = True;
- Widget w_text = (Widget) client_data;
- int index;
- unsigned long buffer_len = 101;
- unsigned long copied_len;
- char format_name_buf[101], str[101];
-
- for(index = 1; index < 11; index++)
- {
- while(( stat = XmClipboardInquireFormat(XtDisplay(w), XtWindow(w_text),
- index, format_name_buf, buffer_len, &copied_len))
- == ClipboardLocked ) ;
- if(stat == ClipboardTruncate)
- printf("format name buffer not long enough to hold data \n");
- else if (stat == ClipboardNoData) break;
- else if(copied_len == 0)
- {
- if(index == 0) printf("Motif Clipboard has no data \n");
- /* else printf("Motif Clipboard has no additional data formats\n"); */
- break;
- }
- else
- {
- if (flag)
- {
- flag = False;
- printf("\n*** Motif Clipboard formats *** \n");
- }
- printf("index %d:", index);
- strncpy(str, format_name_buf, copied_len);
- str[copied_len] = '\0';
- printf("format name len %d, ", copied_len);
- printf("format name %s \n", str);
- }
- }
-
- if(index == 0) printf("no data on Motif Clipboard\n");
- }
-
- void inquireLengthCB(Widget w, caddr_t client_data,
- XmAnyCallbackStruct *call_data)
- {
- int stat, flag = True;
- Widget w_text = (Widget) client_data;
- int index;
- unsigned long buffer_len = 101;
- unsigned long copied_len;
- unsigned long length;
- char format_name_buf[101], str[101];
-
- for(index = 1; index < 11; index++)
- {
- while(( stat = XmClipboardInquireFormat(XtDisplay(w), XtWindow(w_text),
- index, format_name_buf, buffer_len, &copied_len))
- == ClipboardLocked ) ;
- if(stat == ClipboardTruncate)
- printf("format name buffer not long enough to hold data \n");
- else if (stat == ClipboardNoData) break;
- else if(copied_len == 0)
- {
- if(index == 0) printf("Motif Clipboard has no data \n");
- /* else printf("Motif Clipboard has no additional data formats\n"); */
- break;
- }
- else
- {
- if (flag)
- {
- flag = False;
- printf("\n*** Motif Clipboard data lengths by format *** \n");
- }
- printf("index %d:", index);
- strncpy(str, format_name_buf, copied_len);
- str[copied_len] = '\0';
- printf("format name len %d, ", copied_len);
- printf("format name %s, ", str);
-
- while(( stat = XmClipboardInquireLength(XtDisplay(w),
- XtWindow(w_text), str, &length)) == ClipboardLocked ) ;
- if(stat == ClipboardNoData)
- printf("Data is Not Available\n");
- else printf("data length %ld \n", length);
- }
- }
-
- if(index == 0) printf("no data on Motif Clipboard\n");
- }
-
- void inquirePendingItemsCB(Widget w, caddr_t client_data,
- XmAnyCallbackStruct *call_data)
- {
- int stat, flag = True;
- Widget w_text = (Widget) client_data;
- int index, i;
- unsigned long buffer_len = 101;
- unsigned long copied_len;
- char format_name_buf[101], str[101];
- unsigned long icount;
- XmClipboardPendingList ilist;
-
- for(index = 1; index < 11; index++)
- {
- while(( stat = XmClipboardInquireFormat(XtDisplay(w), XtWindow(w_text),
- index, format_name_buf, buffer_len, &copied_len))
- == ClipboardLocked ) ;
- if(stat == ClipboardTruncate)
- printf("format name buffer not long enough to hold data \n");
- else if (stat == ClipboardNoData) break;
- else if(copied_len == 0)
- {
- if(index == 0) printf("Motif Clipboard has no data \n");
- /* else printf("Motif Clipboard has no additional data formats\n"); */
- break;
- }
- else
- {
- if (flag)
- {
- flag = False;
- printf("\n*** Motif Clipboard pending items, by format *** \n");
- }
- printf("index %d:", index);
- strncpy(str, format_name_buf, copied_len);
- str[copied_len] = '\0';
- printf("format name len %d, ", copied_len);
- printf("format name %s:\n ", str);
-
- while(( stat = XmClipboardInquirePendingItems(XtDisplay(w),
- XtWindow(w_text), str, &ilist, &icount ))
- == ClipboardLocked ) ;
- if(icount == 0)
- printf("There are no pending items for this format.\n");
- else
- {
- for( i = 0; i < icount; i++)
- {
- printf("\t pair # %d: data id %d | private id %d \n",
- i, ilist->DataId, ilist->PrivateId);
- }
- XtFree(ilist);
- }
- }
- }
-
- if(index == 0) printf("no data on Motif Clipboard\n");
- }
-
- /* end of xmnotes.c */
-